

#import "RZTabBarController.h"
#import "RZTabBar.h"
#import "SPCommon.h"
#import "SPUpcomingCenterTableViewController.h"
#import "SPDataCenterTableViewController.h"
#import "SPSettingTableViewController.h"
#import "SPNavigationController.h"


@interface RZTabBarController ()<RZTabBarDelegate>

@end

@implementation RZTabBarController

- (RZTabBar *)customTabBar
{
    if(_customTabBar == nil){
        _customTabBar = [[RZTabBar alloc] init];
        _customTabBar.frame = self.tabBar.bounds;
        _customTabBar.delegate = self;
//        _customTabBar.backgroundColor = RZ_COLOR(249, 248, 248);
        _customTabBar.backgroundColor = [UIColor whiteColor];
    }
    return _customTabBar;
}


+(instancetype)defaultTabVc{
    
    static RZTabBarController * tab = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        tab = [[RZTabBarController alloc] init];
        
    });
    return tab;
}




- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    //删除tabBar中自带的子控件
    for (UIView *view in self.tabBar.subviews) {
        //NSLog(@"view----%@----",view);
        
        //UITabBarButton是UITabBarController中各个子控制器在工具条中对应的按钮的称呼
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
}




- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //取消自动留白
    //self.automaticallyAdjustsScrollViewInsets = NO;
    
    //添加自定义tabBar
    //self.customTabBar.backgroundColor = [UIColor redColor];
    //NSLog(@"customTabBar=%@",NSStringFromCGRect(self.customTabBar.frame));
    [self.tabBar addSubview:self.customTabBar];
    
    //初始化所有子控制器
    [self setupAllChildViewController];
    
}




#pragma mark -tabar代理方法
//监听tabBar按钮的改变
- (void)tabBar:(RZTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
    //切换子控制器
    self.selectedIndex = to;
}



/**初始化所有子控制器*/
- (void)setupAllChildViewController
{
    //待办中心
    SPUpcomingCenterTableViewController *upcoming = [[SPUpcomingCenterTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self setupChildViewController:upcoming title:@"待办中心" imageName:@"icon_upcoming" selectedImageName:@"icon_upcoming_selected"];
    
    //数据中心
    SPDataCenterTableViewController *data = [[SPDataCenterTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self setupChildViewController:data title:@"数据中心" imageName:@"icon_data" selectedImageName:@"icon_data_selected"];
    
    //设置
    SPSettingTableViewController *setting = [[SPSettingTableViewController alloc] init];
    [self setupChildViewController:setting title:@"设置" imageName:@"icon_setting" selectedImageName:@"icon_setting_selected"];
    
}




/**初始化一个子控制器*/
- (void)setupChildViewController:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    
    //1 包装一个导航控制器
    SPNavigationController *nav = [[SPNavigationController alloc] initWithRootViewController:childVC];
    nav.navBarHairlineImageView.hidden = YES;
    [self addChildViewController:nav];

    
    //2 设置子控制器属性
    //设置标题
    childVC.title = title;
    
    //设置图标
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    
    //选中图标的设置
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    if([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0){ //图片不需要进行渲染
        childVC.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    }else{
        childVC.tabBarItem.selectedImage = selectedImage;
    }
    
    //3 添加TabBar内部的按钮( UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定)
    [self.customTabBar addTabBarButtonWithItem:childVC.tabBarItem];
    
}




//设置设备方向
//是否支持旋转
-(BOOL)shouldAutorotate
{
    return self.selectedViewController.shouldAutorotate;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.selectedViewController.supportedInterfaceOrientations;
}

@end
